package com.hnchances.grade.common.AOP;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hnchances.grade.common.utils.JWTUtils;
import com.hnchances.grade.entity.UserEntity;
import com.hnchances.grade.service.UserService;
import com.hnchances.grade.shiro.ShiroRealm;
import com.hnchances.grade.vo.UpdateUserVo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;


@Slf4j
@Component
@Aspect
public class FillCommonInfoAdvice {
    @Autowired
    private UserService userService;
    //定义一个要插入的地方
    @Pointcut("execution(* com.hnchances.grade.controller.UserController.update(*,*))")
//    @Pointcut("execution(* com.hnchances.grade.service.impl.UserServiceImpl.update*(*))")
    public void pointcut() {
    }

    //定义一个切入点
    @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        log.info("进去切面");
        Object[] args = joinPoint.getArgs();
        UpdateUserVo updateUserVo = (UpdateUserVo) args[0];
        //当前登陆的人是：
        //设置修改人id
        ShiroRealm shiroRealm = new ShiroRealm();
        String token = (String) args[1];
        String username = JWTUtils.getUsername(token);
        UserEntity one = userService.getOne(new QueryWrapper<UserEntity>().eq("userName", username));
        updateUserVo.setUpdateById(one.getId());
        //设置时间
        updateUserVo.setUpdateDateTime(new Date());
    }

//    @After("pointcut()")
//    public void after() {
//        System.out.println("方法之后");
//    }
//
//    @AfterReturning(value = "pointcut()", returning = "result")
//    public void Return(Object result) {
//        System.out.println("修改方法正常执行" + "==" + "返回了" + result);
//    }
//
//    @AfterThrowing(value = "pointcut()", throwing = "exception")
//    public void Throw(JoinPoint joinPoint, Exception exception) {
//        String name = joinPoint.getSignature().getName();//返回方法名
//        System.out.println(name + "方法异常:" + exception);
//
//    }


}
